package com.lc.luzhengtang.adapter;

import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by ${junLong} on 2017/4/12.
 */

public class ShoppingCarAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private TextView tvGoodsNum;
    private String flag = null;
//    private SmoothCheckBox mScb;
    public Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public ShoppingCarAdapter(int layoutResId, List<String> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final String item) {
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) helper.convertView);
        initView(helper, item);
    }

    private void initView(final BaseViewHolder helper, final String item) {
        //显示数据
     //   Glide.with(mContext).load(new StringBuilder().append(Conn.IMAGEHOST).append(item.getPicurl()).toString()).into((ImageView) helper.getView(R.id.iv_show_goods));
    /*    helper.setText(R.id.tv_goods_title, item.getTitle());
        helper.setText(R.id.tv_goods_num, String.valueOf(item.getNum()));
        helper.setText(R.id.tv_goods_money, new StringBuilder().append("￥").append(String.valueOf(item.getMoney())).toString());
        //获取Id
        mScb = helper.getView(R.id.scb);
        ImageButton ibUp = helper.getView(R.id.ib_up);
        ImageButton ibDown = helper.getView(R.id.ib_down);
        tvGoodsNum = helper.getView(R.id.tv_goods_num);
        //设置tag防止数据混乱
        helper.setTag(R.id.tv_goods_num, helper.getAdapterPosition());
        helper.setTag(R.id.tv_goods_money, helper.getAdapterPosition());
        helper.setTag(R.id.tv_goods_title, helper.getAdapterPosition());
        helper.setTag(R.id.scb, helper.getAdapterPosition());
        helper.setTag(R.id.iv_show_goods, helper.getAdapterPosition());
        //数量加减
        ibUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = item.getNum() + 1;
                item.setNum(i);
                requestChangeNum(helper, item);
                flag = "0";
            }
        });
        ibDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.getNum() > 1) {
                    int i = item.getNum() - 1;
                    item.setNum(i);
                    helper.setText(R.id.tv_goods_num, String.valueOf(i));
                    requestChangeNum(helper, item);
                    flag = "1";
                }
            }
        });

        mScb.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked) {


                item.setSelected(isChecked);
                listener.startCalculate();

            }
        });
        mScb.setChecked(item.getSelected(), false);*/
    }

    private void requestChangeNum(final BaseViewHolder helper, final String item) {

     /*   new GetUpdateCartGoodsNum(String.valueOf(item.getId()), String.valueOf(item.getUser_id()), String.valueOf(item.getNum()), item.getGoodsattr(), new AsyCallBack<CartBeanResp>() {
            @Override
            public void onSuccess(String toast, int type, Object o, CartBeanResp cartBeanResp) throws Exception {
                super.onSuccess(toast, type, o, cartBeanResp);
                helper.setText(R.id.tv_goods_num, String.valueOf(item.getNum()));
                listener.startCalculate();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                ToastUtils.showToast(mContext, "更改失败");
                switch (flag) {
                    case "0":
                        item.setNum(item.getNum() - 1);
                        break;
                    case "1":
                        item.setNum(item.getNum() + 1);
                        break;
                }
            }
        }).execute(mContext, false);*/
    }


    public interface Listener {
        void startCalculate();
    }

}
